package com.example.cinemaorder.consumer;
import com.example.cinemaorder.entity.SysSession;
import com.example.cinemaorder.entity.vo.SysBillVo;
import com.example.cinemaorder.service.SysBillService;
import com.example.cinemaorder.service.SysSessionService;
import com.example.cinemaorder.util.ResponseResult;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @author 高垚淼
 * @version 1.0
 */
@Component
public class GetOrderConsumer {

    @Autowired
    SysSessionService sysSessionService;
    @Autowired
    SysBillService sysBillService;
    @Autowired
    RedisTemplate redisTemplate;


    @RabbitListener(queues = "getOrder")
    public Object dealEmail(SysBillVo sysBillVo, Message message, Channel channel) throws IOException {
        try {
            // 更新售出座位数量、场次座位信息
            int updateSallNums = sysBillVo.getSysBill().getSeats().split(",").length;
            sysSessionService.updateSeats(sysBillVo.getSysBill().getSessionId(),updateSallNums,sysBillVo.getSessionSeats());
            // 插入订单记录
            //  创建时间
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date();
            String formatDate = simpleDateFormat.format(date);
            sysBillVo.getSysBill().setCreateTime(formatDate);
            //  订单失效时间
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
                 // 在Calendar对象上加上15分钟
            calendar.add(Calendar.MINUTE, 15);
                 // 将Calendar对象转换为Date对象
            Date newDate = calendar.getTime();
            String formatNewDate = simpleDateFormat.format(newDate);
            sysBillVo.getSysBill().setDeadline(formatNewDate);
            // 取消时间默认空
            // 设置支付状态
            sysBillVo.getSysBill().setPayState(0);
            // 设置退单状态
            sysBillVo.getSysBill().setCancelState(0);
            //  插入订单记录
            sysBillService.insertBill(sysBillVo.getSysBill());
            // 给下个页面数据，根据查询所有信息
            if (sysBillVo.getSysBill().getBillId()!=null) {
                // 以座位为键，给下个页面数据，根据查询所有信息
                redisTemplate.opsForValue().set(sysBillVo.getSysBill().getSeats(),sysBillVo.getSysBill(),16, TimeUnit.MINUTES);
                return new ResponseResult(200,"插入订单记录成功",sysBillVo.getSysBill().getBillId());
            }
            return  new ResponseResult(400,"插入订单记录失败，后端错误",null);
        } catch (Exception e) {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
            e.printStackTrace();
            return  new ResponseResult(400,"插入订单记录失败，未知异常",null);
        }
    }
}
